import Vue from 'vue'
import Vuex from 'vuex'
import { userInfo } from '@/api/user.js'
import { systemInfo, upgrade } from '@/api/home.js'

import i18n from '@/lang'

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		systemInfo: '{}',
		token: uni.getStorageSync('oned_token') || '',
		language: uni.getStorageSync('language') || 'english',
		userInfo: uni.getStorageSync('userinfo') || '{}',
		upgrade: uni.getStorageSync('upgrade') || '{}',
		address: [],
		isLogin: false,
		recommValue: true,
		clientid: '',
		takeIcon: 2,
		tabList: [],
		network: '',
		platform: '',
	},
	getters: {
		token: state => state.token,
		language: state => state.language,
		userInfo: state => JSON.parse(state.userInfo),
		upgrade: state => JSON.parse(state.upgrade),
		address: state => state.address,
		systemInfo: state => JSON.parse(state.systemInfo),
		recommValue: state => state.recommValue,
		network: state => state.network,
		platform: state => state.platform
	},
	mutations: {
		SET_TABLIST(state, data) {
			state.tabList = data
		},
		SET_TAKEICON(state, data) {
			state.takeIcon = data
		},
		SET_CLIENTID(state, data) {
			state.clientid = data
		},
		SET_RECOMM(state, data) {
			state.recommValue = data
		},
		SET_PLATFORM(state, data) {
			state.platform = data
		},
		SET_LANGUAGE(state, data) {
			state.language = data
		},
		SET_ADDRESS(state, data) {
			state.address = data
		},
		SET_TOKEN(state, data) {
			state.token = data
		},
		SET_CID(state, data) {
			state.cid = data
		},
		SET_SYSTEM_INFO(state, data) {
			state.systemInfo = data
		},
		SET_USER_INFO(state, data) {
			state.userInfo = data
		},
		SET_ISLOGIN(state, data) {
			state.isLogin = data
		},
		SET_USER_UPGRADE(state, data) {
			state.upgrade = data
		},
		SET_NETWORK(state, data) {
			state.network = data.networkType;
		}
	},
	actions: {
		setAddress({ commit, dispatch }, data) {
			commit('SET_ADDRESS', data)
			// dispatch('setTabBarItem')
			// uni.setStorage({
			// 	key: 'iqua.language',
			// 	data
			// })
		},
		setLanguage({ commit, dispatch }, data) {
			commit('SET_LANGUAGE', data)
			// dispatch('setTabBarItem')
			// uni.setStorage({
			// 	key: 'iqua.language',
			// 	data
			// })
		},
		setToken({ commit, dispatch }, token) {
			commit('SET_TOKEN', token)
			uni.setStorageSync('oned_token', token)
			// dispatch('getUserInfo').then(() => {
			// 
			// })
		},
		setUserInfo({ commit, dispatch }, res) {
			commit('SET_USER_INFO', JSON.stringify(res))
			uni.setStorageSync('userinfo',  JSON.stringify(res))
			if (res.level_log == 1) {
				dispatch('getUserUpgrade')
			}
		},
		removeToken({ commit }) {
			commit('SET_TOKEN', '')
			commit('SET_USER_INFO', '{}')
			// commit('SET_SYSTEM_INFO', '{}')
			uni.removeStorageSync('oned_token')
			uni.removeStorageSync('userinfo')
		},
		getUserInfo({ commit, dispatch }) {
			return new Promise((resolve, reject) => {
				userInfo().then(res => {
					commit('SET_USER_INFO', JSON.stringify(res))
					uni.setStorageSync('userinfo',  JSON.stringify(res))
					commit('SET_ISLOGIN', true)
					if (res.level_log == 1) {
						dispatch('getUserUpgrade')
					}
					resolve(res)
				}).catch(err => {
					commit('SET_ISLOGIN', false)
					reject(err)
				})
			})
		},
		getUserUpgrade({ commit }) {
			return new Promise((resolve, reject) => {
				upgrade().then(res => {
					commit('SET_USER_UPGRADE', JSON.stringify(res))
					uni.setStorageSync('upgrade',  JSON.stringify(res))
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		checkLogin({ state, dispatch }) {
			return new Promise((resolve, reject) => {
				if (state.isLogin) {
					resolve()
				} else {
					console.log('no login store')
					dispatch('getUserInfo').then(() => {
						resolve()
					}).catch(() => {
						reject()
					})
				}
			})
		},
		getSystemInfo({ commit }) {
			return new Promise((resolve, reject) => {
				systemInfo().then(res => {
					// uni.setNavigationBarTitle({
					// 	title: res.title
					// })
					commit('SET_SYSTEM_INFO', JSON.stringify(res))
					// commit('SET_TAKEICON', res.taskstatus)
					// commit('SET_TABLIST', res.tablist)
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		setTabBarItem({ state }) {
			// uni.setTabBarItem({
			// 	index: 0,
			// 	text: i18n.messages[state.language].tabBar.home
			// })
			// uni.setTabBarItem({
			// 	index: 1,
			// 	text: i18n.messages[state.language].tabBar.investment
			// })
			// uni.setTabBarItem({
			// 	index: 2,
			// 	text: i18n.messages[state.language].tabBar.team
			// })
			// uni.setTabBarItem({
			// 	index: 3,
			// 	text: i18n.messages[state.language].tabBar.mine
			// })
		},
		updateNetwork( { commit }, res) {
			commit('SET_NETWORK', res)
		}
	}
});

export default store
